package Test2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6060);

        while (true) {
            //准备接收包裹
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            socket.receive(packet); //阻塞式接收包裹

            //输出数据
            byte[] data = packet.getData();
            String msg = new String(data, 0, packet.getLength());
            System.out.println(msg);

            //断开连接条件
            if (msg.equals("bye")) break;
        }
        //关闭连接
        socket.close();
    }
}
